package com.ys.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ys.entity.WalletWithdraw;
import com.ys.entity.block.Transaction;
import com.ys.service.WalletWithdrawServiceImap;
import com.ys.service.wallet.IWalletJobService;
import com.ys.service.wallet.WalletJobServiceContent;
import com.ys.service.wallet.WalletServicceContext;
import com.ys.service.wallet.base.IWalletService;
import com.ys.util.HexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
@Slf4j
public class WalletJob {

    @Autowired
    private WalletServicceContext walletServicceContext;
    @Autowired
    private WalletWithdrawServiceImap walletWithdrawServiceImap;
    @Autowired
    private WalletJobServiceContent walletJobServiceContent;




    /**
     * 发送提币交易定时任务
     * 重点：提币交易一定不要自动重试 并且同一个交易也不要重复执行
     */
    @Scheduled(cron = "5 * * * * ?")
    public void sendWithDraw(){
        log.info("定时发送提币交易 start");
        EntityWrapper<WalletWithdraw> ew=new EntityWrapper<>();
        ew.isNull("txHash");
        List<WalletWithdraw> list=walletWithdrawServiceImap.selectList(ew);
        for(WalletWithdraw walletWithdraw:list){
            IWalletService walletService=walletServicceContext.getWalletRPC(walletWithdraw.getCurrency());
            WalletWithdraw updateTxHash=new WalletWithdraw();
            updateTxHash.setId(walletWithdraw.getId());
            //先设置 交易hash
            updateTxHash.setTxHash("-1");
            walletWithdrawServiceImap.updateById(updateTxHash);
            String txHash=walletService.sendWithDraw(walletWithdraw);
            if(StrUtil.isBlank(txHash)){
                log.error("发送提币交易失败，walletWithdraw="+JSONUtil.toJsonStr(walletWithdraw));
                continue;
            }
            /**更新交易hash**/
            updateTxHash.setTxHash(txHash);
            walletWithdrawServiceImap.updateById(updateTxHash);
        }
        log.info("定时发送提币交易 end");
    }


    /**
     * 查询提币交易的打包的blcoknum 为确认交易状态做准备
     */
    @Scheduled(cron = "5 * * * * ?")
    public void updateWithDrawBlockNum(){
        log.info("更新提币交易的blocknum任务开始 start");
        EntityWrapper<WalletWithdraw> ew = new EntityWrapper<>();
        ew.isNull("blockNum");
        ew.isNotNull("txHash");
        List<WalletWithdraw> list=walletWithdrawServiceImap.selectList(ew);
        if(CollUtil.isEmpty(list)){
            log.info("提币更新blocknum的任务为空");
            return ;
        }
        for(WalletWithdraw walletWithdraw:list){
            if(walletWithdraw.getTxHash().equals("-1")){
                log.warn("提币失败请检查后确定是否重新发起提币：walletWithdraw"+JSONUtil.toJsonStr(walletWithdraw));
                continue;
            }

            IWalletService walletService=walletServicceContext.getWalletRPC(walletWithdraw.getCurrency());
            IWalletJobService walletJobService= walletJobServiceContent.getWalletJobService(walletWithdraw.getCurrencyChainType());
            Transaction t= walletService.queryTransactionByHash(walletWithdraw.getTxHash(),walletJobService.getTransaction());
            walletJobService.dealTransaction(t,walletWithdraw);
        }
        log.info("更新提币交易的blocknum任务开始 end");
    }








}
